package com.tanhua.server.service;

/**
 * @Author: cl
 * @Date: 2022/5/2 - 05 - 02
 * @Description:
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;

/**
 * 通用设置模块
 */
@Service
public class SettingsService {

    //设置模块服务
    @DubboReference
    private SettingsApi settingsApi;

    //问题模块服务
    @DubboReference
    private QuestionApi questionApi;

    //黑名单服务
    @DubboReference
    private BlackListApi blackListApi;

    /**
     * 查询通用设置
     *
     * @return
     */
    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();

        //1、查询用户id并封装
        Long userId = UserHolder.getUserId();
        settingsVo.setId(userId);
        //2、查询用户手机号并封装
        String mobile = UserHolder.getMobile();
        settingsVo.setPhone(mobile);
        //3、查询设置并封装
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        //4、查询问题并封装
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢我吗？" : question.getTxt();
        settingsVo.setStrangerQuestion(txt);

        return settingsVo;
    }

    /**
     * 添加陌生人问题
     *
     * @param content
     */
    public void saveQuestion(String content) {
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、调用api查询用户是否已经设置问题
        Question q = questionApi.findByUserId(userId);
        //3、判断问题是否存在
        if (q == null) {
            //3.1不存在进行保存
            Question question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.save(question);
        } else {
            //3.2存在进行更新
            q.setTxt(content);
            questionApi.update(q);
        }
    }

    /**
     * 添加设置信息
     *
     * @param settings
     */
    public void saveSettings(Settings settings) {

        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、查询用户是否有设置信息
        Settings settings1 = settingsApi.findByUserId(userId);
        //3、判断有还是没有
        if (settings1 == null) {
            //3.1没有就新增
            Settings settings2 = new Settings();
            settings2.setUserId(userId);
            settings2.setGonggaoNotification(settings.getGonggaoNotification());
            settings2.setLikeNotification(settings.getLikeNotification());
            settings2.setPinglunNotification(settings.getPinglunNotification());
            settingsApi.save(settings2);
        } else {
            //3.2有就更改
            settings1.setGonggaoNotification(settings.getGonggaoNotification());
            settings1.setLikeNotification(settings.getLikeNotification());
            settings1.setPinglunNotification(settings.getPinglunNotification());
            settingsApi.update(settings1);
        }


    }

    /**
     * 分页展示黑名单列表
     *
     * @param page
     * @param size
     * @return
     */
    public PageResult blackList(int page, int size) {
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、调用黑名单服务查询黑名单列表
        IPage<UserInfo> byPage = blackListApi.findByPage(userId, page, size);
        //3、将获取到的对象封装为vo对象返回
        PageResult pageResult = new PageResult(page, size, byPage.getTotal(), byPage.getRecords());

        return pageResult;
    }

    /**
     * 移除黑名单用户
     *
     * @param blackUserId
     */
    public void delete(Long blackUserId) {

        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、调用服务删除
        blackListApi.delete(userId,blackUserId);
    }
}
